package io.kiki.sba.registry.server.metadata.remoting.connection;


import io.kiki.sba.registry.api.Channel;
import io.kiki.sba.registry.api.Server;
import io.kiki.sba.registry.api.exchange.DataExchanger;
import io.kiki.sba.registry.server.metadata.bootstrap.config.ServerConfig;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public abstract class AbstractNodeConnectManager implements NodeConnectManager {
    @Autowired
    protected DataExchanger boltDataExchanger;
    @Autowired
    protected ServerConfig serverConfig;

    @Override
    public Collection<InetSocketAddress> getChannelRemoteAddressList(String dataCenter) {
        Server server = boltDataExchanger.getServer(getServerPort());
        if (server == null) {
            return Collections.emptyList();
        }
        List<Channel> channelList = server.getChannelList();
        List<InetSocketAddress> list = new ArrayList<>(channelList.size());
        for (Channel channel : channelList) {
            list.add(channel.getRemoteAddress());
        }
        return list;
    }

    protected abstract int getServerPort();
}
